package com.xoa.dev.efficiency.service.impl;

import com.xoa.dev.efficiency.dao.FlowSettingHolidayMapper;
import com.xoa.dev.efficiency.service.FlowSettingHolidayService;
import com.xoa.model.users.Users;
import com.xoa.dev.efficiency.model.FlowSettingHoliday;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*
Created by wuwei on 2018-07-22 0:47
* */

@Service
public class FlowSettingHolidayServiceImpl implements FlowSettingHolidayService {
    @Resource
    private FlowSettingHolidayMapper flowSettingHolidayMapper;
    @Override
    public ToJson<FlowSettingHoliday> insertFlowSettingHoliday(FlowSettingHoliday flowsettingholiday, Users user){
        ToJson<FlowSettingHoliday> json = new ToJson<FlowSettingHoliday>();
        try{
            String userId = user.getUserId();
            if (userId.trim().equals("admin")){
                flowSettingHolidayMapper.insertFlowSettingHoliday(flowsettingholiday);
                json.setFlag(0);
                json.setMsg("ok");
            }else{
                json.setFlag(1);
                json.setMsg("非系统管理员用户，禁止操作!");
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowSettingHoliday> selectFlowSettingHoliday(FlowSettingHoliday flowsettingholiday){
        ToJson<FlowSettingHoliday> json = new ToJson<FlowSettingHoliday>();
        try{
            String beginyear = flowsettingholiday.getBeginyear();
            List<FlowSettingHoliday> flowsettingholidayList = flowSettingHolidayMapper.selectFlowSettingHoliday(beginyear);
            json.setObj(flowsettingholidayList);
            json.setFlag(0);
            json.setMsg("OK");
        }catch(Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
    public ToJson<FlowSettingHoliday> selectFlowSettingHolidayPagination( FlowSettingHoliday flowsettingholiday,Map<String, Object> maps, Integer page, Integer pageSize){

        ToJson<FlowSettingHoliday> json = new ToJson<FlowSettingHoliday>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        //pageParams.setUseFlag(beginyear);
        maps.put("page", pageParams);

        maps.put("flowsettingholiday",flowsettingholiday);
        try{
            List<FlowSettingHoliday> flowsettingholidayList = flowSettingHolidayMapper.selectFlowSettingHolidayPagination(maps);
            json.setTotleNum(pageParams.getTotal());
            json.setObj(flowsettingholidayList);
            json.setFlag(0);
            json.setMsg("ok");
        }catch(Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
    public ToJson<FlowSettingHoliday> deleteByPrimaryKey(Integer holidayId){

        ToJson<FlowSettingHoliday> json = new ToJson<FlowSettingHoliday>();
        try{
            flowSettingHolidayMapper.deleteByPrimaryKey(holidayId);
            json.setFlag(0);
            json.setMsg("ok");
        }catch(Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }

    public ToJson<FlowSettingHoliday> updateByPrimaryKey(FlowSettingHoliday flowsettingholiday){

        ToJson<FlowSettingHoliday> json = new ToJson<FlowSettingHoliday>();
        try{
            flowSettingHolidayMapper.updateByPrimaryKey(flowsettingholiday);
            json.setFlag(0);
            json.setMsg("ok");
        }catch(Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
        }
        return json;
    }
}
